﻿using System.IO;

namespace OpenSage.Gui.Apt.ActionScript;

/// <summary>
/// Url handler
/// </summary>
public static class UrlHandler
{
    private static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
    public static void Handle(VM.HandleCommand cmdHandler, VM.HandleExternalMovie movieHandler, ActionContext context, string url, string target)
    {
        Logger.Debug($"[URL] URL: {url} Target: {target}");

        if (url.StartsWith("FSCommand:"))
        {
            var command = url.Replace("FSCommand:", "");

            cmdHandler(context, command, target);
        }
        else
        {
            //DO STUFF
            var targetObject = context.Scope.Variables[target].ToObject();

            if (!(targetObject.Item is SpriteItem))
            {
                Logger.Error("[URL] Target must be a sprite!");
            }

            var targetSprite = targetObject.Item as SpriteItem;
            var aptFile = movieHandler(url);
            var oldName = targetSprite.Name;

            targetSprite.Create(aptFile.Movie, targetSprite.Context, targetSprite.Parent);
            targetSprite.Name = oldName;
        }
    }
}
